{% extends "base.html" %}
{% block body %}
{% import "macros.html" as macros %}
<div class="contact-info">

    <ol class="breadcrumb">
        <li><a href="{{ url_for('pic.home') }}">Home</a></li>
        <li><a href="{{ url_for('work.home') }}">Work</a></li>
        <li class="active">Work:{{ title }}</li>
    </ol>

    <div class="contact-grids">
            <ul class="list3">
                <li>
                    <div class="extra-wrap">
                        <form id="edit_work_form">
                            <input type="text" class="form-control hidden" value="{{page.id}}" name="work_id">
                            <div class="form-group">
                                <label for="work_title">标题</label>
                                <input type="text" class="form-control" id="work_title" value="{{page.title}}" name="work_title" readonly>
                            </div>

                            <div class="form-group row">
                                <div class="col-xs-12 col-sm-6">
                                    <label for="work_title">创建时间</label>
                                    <input type="text" class="form-control" value="{{page.create_time}}" disabled>
                                </div>
                                <div class="col-xs-12 col-sm-6">
                                    <label for="work_title">更新时间</label>
                                    <input type="text" class="form-control" value="{{page.update_time | default('', true)}}" disabled>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="work_content">内容</label>
                                <textarea id="work_content" class="form-control" rows="8" name="work_content" readonly>{{page.content}}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="work_priority">优先级</label>
                                <input type="number" class="form-control" id="work_priority" name="work_priority" value="{{page.priority}}" min="1" max="3" readonly>
                            </div>
                            <div class="form-group">
                                <span class="pull-right">
                                    <a href="{{url_for('work.work_page', page_id=page.id)}}" class="btn btn-sm btn-default">返回</a>
                                    <input type="button" class="btn btn-sm btn-default" value="修改" id="page_edit_btn">
                                    <input type="button" class="btn btn-sm btn-default hidden" value="取消" id="cancel_edit_btn">
                                    <input type="button" class="btn btn-sm btn-primary hidden" value="保存" id="edit_submit_btn">
                                </span>
                                <input type="button" class="btn btn-sm btn-danger pull-left" value="删除" id="page_del_btn">
                            </div>
                        </form>
                    </div>
                </li>
            </ul>

        <div class="clear"></div>

    </div>
</div>
{% endblock %}

{% block script %}
<script type="text/javascript">
$('#page_edit_btn').click(function () {
    $('.form-control').removeAttr('readonly')
    $('#cancel_edit_btn').removeClass('hidden')
    $('#edit_submit_btn').removeClass('hidden')
    $(this).addClass('hidden')
})

$('#cancel_edit_btn').click(function () {
    $(this).addClass('hidden')
    $('#edit_submit_btn').addClass('hidden')
    $('#page_edit_btn').removeClass('hidden')
    $('#work_title').attr("readonly",true)
    $('#work_content').attr("readonly",true)
    $('#work_priority').attr("readonly",true)
})

$('#edit_submit_btn').click(function () {
    if ($('#work_title').val() != '' && $('#work_content').val() !=''){
        var data = $('#edit_work_form').serialize()
        $.ajax({
            url: "{{url_for('api.work_edit')}}",
            type: "POST",
            dataType: "json",
            data: data,
            async:false,
            success: function(result) {
                if (result['status'] == 1){
                    window.location.href="{{url_for('work.work_page',page_id=page.id)}}"
                }
                else{alert(result['msg'])}
            }
        })
    }else{alert('标题或内容不能为空')}
})

$('#page_del_btn').click(function () {
    var msg = "确定删除 {{page.title}} 吗？";
    if (confirm(msg)==true){
        $.ajax({
            url: "{{url_for('api.work_del')}}",
            type: 'GET',
            data: 'id=' + {{page.id}},
            dataType:"json",
            async:false,
            success: function (result) {
                if (result['status'] == 1){
                    window.location.href="{{url_for('work.home')}}"
                }
                else{ alert(result['msg']) }
            }
        })
    }
    else{}

})


</script>
{% endblock %}